﻿using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using CustomWcfContext;

namespace SilverlightApplication1.Web
{
	/// <summary>
	/// This is sample class to do some extra work at the WCF chain.
	/// </summary>
	public class SomeDispatchMessageInspector : IDispatchMessageInspector
	{

		#region Implementation of IDispatchMessageInspector

		public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
		{
			// Your any state.
			var someState = new YourState
				{
					SomeValue = "SomeValue",
					OtherValue = "123",
				};


			if (Debugger.IsAttached)
			{
				Debugger.Break();
			}


			// Here's how to set your state to the WCF context at any/start point of WCF chain.
			WcfContext<YourState>.SetCurrent(someState);

			return null;
		}

		public void BeforeSendReply(ref Message reply, object correlationState)
		{
			// Example of taking your state out and how to work with it.

			if (Debugger.IsAttached)
			{
				Debugger.Break();
			}


			// Do something if your state is not a value type (for refference type).
			var state = WcfContext<YourState>.Current;
			state.SomeValue += String.Format(" {0}", DateTime.Now.ToString());
			
			// Do something if your state is a value type (struct, int, etc).
			WcfContext<YourState>.Current.SomeValue += String.Format(" +++ {0}", DateTime.Now.ToString());


			var dummyResult = WcfContext<YourState>.Current.SomeValue;

			if(Debugger.IsAttached)
			{
				Debugger.Break();
			}

		}

		#endregion

	}
}